package com.hw.core.system.controller;

import com.hw.core.system.entity.Course;
import com.hw.core.system.entity.NoteBook;
import com.hw.core.system.service.CourseESService;
import com.hw.core.system.service.NoteBookESService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("ES")
public class ESController {

    @Autowired(required = false)
    private NoteBookESService noteBookESService;
    @Autowired
    private CourseESService courseESService;


    @GetMapping("deleteNoteBookAll")
    @RequiresPermissions(value = "笔记清索引")
    @ResponseBody
    public String deleteNoteBookAll() {
        noteBookESService.deleteAll();
        return "清除索引成功";
    }

    @GetMapping("deleteCourseAll")
    @RequiresPermissions(value = "课程清索引")
    @ResponseBody
    public String deleteCourseAll() {
        courseESService.deleteAll();
        return "清除索引成功";
    }

    @GetMapping("addNoteBookIndex")
    @ResponseBody
    @RequiresPermissions(value = "笔记创索引")
    public String deleteNoteBookIndex() {


        noteBookESService.addAll();

        return "创建索引成功";
    }

    @GetMapping("addCourseIndex")
    @ResponseBody
    @RequiresPermissions(value = "课程创索引")
    public String addCourseIndex() {
        courseESService.addAll();
        return "创建索引成功";
    }

    @GetMapping("courseFind")
    @RequiresPermissions(value = "检索课程")
    public String find(String key, Model model, Integer page) throws Exception {
        Page<Course> coursePage = null;
        if (page == null) {
            if (null != key && !("".equals( key ))) {
                coursePage = courseESService.find( key, 1, 16 );
            } else {
                coursePage = courseESService.find( " ", 1, 16 );
            }

        } else {
            if (page < 1)
                throw new Exception( "参数错误" );
            if (null != key && key != "") {
                coursePage = courseESService.find( key, page, 16 );
            } else {
                coursePage = courseESService.find( " ", page, 16 );
            }
        }
        model.addAttribute( "key", key );
        model.addAttribute( "coursePage", coursePage );
        return "ES/course";
    }

    @GetMapping("noteBookFind")
    @RequiresPermissions(value = "检索笔记")
    public String noteBookFind(String key, Model model, Integer page) throws Exception {
        Page<NoteBook> noteBookPage = null;
        if (page == null) {
            if (null != key && !("".equals( key ))) {
                noteBookPage = noteBookESService.find( key, 1, 16 );
            } else {
                noteBookPage = noteBookESService.find( " ", 1, 16 );
            }

        } else {
            if (page < 1)
                throw new Exception( "参数错误" );
            if (null != key && key != "") {
                noteBookPage = noteBookESService.find( key, page, 16 );
            } else {
                noteBookPage = noteBookESService.find( " ", page, 16 );
            }
        }
        model.addAttribute( "key", key );
        model.addAttribute( "noteBookPage", noteBookPage );
        return "ES/noteBook";
    }

    @GetMapping("{name}")
    @RequiresPermissions(value = "全文检索")
    public String view(@PathVariable String name) {
        return name;
    }


}
